package com.example.imtoken.modules.user.create.backups.mnemonic.confirmation

import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.App
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.Utils
import com.example.imtoken.databinding.ActivityConfirmationMnemonicsBinding
import com.example.imtoken.modules.main.MainActivity
import com.xuexiang.xui.widget.popupwindow.bar.CookieBar

/**
 * 确认助记词
 */
class ConfirmationMnemonicsActivity :
    BaseActivity<ConfirmationMnemonicsViewModel, ActivityConfirmationMnemonicsBinding>(
        ConfirmationMnemonicsViewModel::class.java
    ) {

    private val mnemonic by lazy {
        intent.getStringExtra("mnemonic")
    }

    private lateinit var adapter1: ConfirmationMnemonicsAdapter
    private lateinit var adapter2: ConfirmationMnemonicsAdapter2

    override fun initLayoutId(): Int {
        return R.layout.activity_confirmation_mnemonics
    }

    override fun init() {
        dataBinding.headView.vLine.visibility = View.INVISIBLE

        initClick()

        initAdapter()
        initObserve()
        viewModel.initWords(mnemonic)
    }

    private fun initAdapter() {
        adapter1 = ConfirmationMnemonicsAdapter(R.layout.item_mnemonics_txt)
        adapter1.setDiffCallback(ConfirmationMnemonicsDiffCallback())
        dataBinding.rvWords.layoutManager = Utils.getFlexboxLayoutManager(this)
        dataBinding.rvWords.adapter = adapter1
        dataBinding.rvWords.itemAnimator = null
        adapter1.setOnItemClickListener { _, _, position ->
            viewModel.setSelect(position)
        }

        adapter2 = ConfirmationMnemonicsAdapter2(R.layout.item_mnemonics_txt2)
        adapter2.setDiffCallback(ConfirmationMnemonicsDiffCallback())
        dataBinding.rvSelectWords.layoutManager = Utils.getFlexboxLayoutManager(this)
        dataBinding.rvSelectWords.adapter = adapter2
        adapter2.setOnItemClickListener { _, _, position ->
            viewModel.removeSelectWords2(adapter2.data[position])
        }
    }

    private fun initObserve() {
        viewModel.wordsLiveData.observe(this) {
            adapter1.setDiffNewData(it)
        }
        viewModel.selectWordsLiveData.observe(this) {
            adapter2.setDiffNewData(it)
        }
        viewModel.selectResult.observe(this) {
            if (it) {
                dataBinding.tvSuccess.setBackgroundResource(R.drawable.shape_create_btn_bg_true)
            } else {
                dataBinding.tvSuccess.setBackgroundResource(R.drawable.shape_create_btn_bg_false)
            }
            dataBinding.tvSuccess.isClickable = it
        }
        viewModel.showErrorMsg.observe(this) {
            if (it) {
                CookieBar.builder(this)
                    .setMessage(R.string.confirmationMnemonicsTips1)
                    .setBackgroundColor(R.color.xui_config_color_red)
                    .setMessageColor(R.color.white)
                    .show()
                viewModel.showErrorMsg.value = false
            }
        }
    }

    private fun initClick() {
        dataBinding.headView.ivHeadBack.setOnClickListener { this.finish() }
        ClickUtils.applyGlobalDebouncing(dataBinding.tvSuccess) {
            ToastUtils.showShort(R.string.backupSuccess)
            if (mnemonic == null) {
                viewModel.addBackUp()
                if (App.newCreateData != null) {
                    App.newCreateData = null
                    ActivityUtils.finishToActivity(MainActivity::class.java, false)
                } else {
                    ActivityUtils.startActivity(MainActivity::class.java)
                }
            } else {
                this.finish()
            }
        }
    }
}